2 // IconFamily class interface
3 // by Troy Stephens, Thomas Schnitzer, David Remahl, Nathan Day and Ben Haller
7 // http://homepage.mac.com/troy_stephens/software/objects/IconFamily/
9 // Problems, shortcomings, and uncertainties that I'm aware of are flagged
10 // with "NOTE:". Please address bug reports, bug fixes, suggestions, etc.
11 // to me at troy_stephens@mac.com
13 // This code is provided as-is, with no warranty, in the hope that it will be
14 // useful. However, it appears to work fine on Mac OS X 10.1.4. :-)
16 //IconFamily depends heavily upon Carbon calls and thus will only work in Mac OS X
17 #ifdef MAC_OS_X_VERSION_10_0
19 #import <Carbon/Carbon.h>
21 // This class is a Cocoa/Objective-C wrapper for the Mac OS X Carbon API's
22 // "icon family" data type. Its main purpose is to enable Cocoa applications
23 // to easily create custom file icons from NSImage instances, and thus take
24 // advantage of Mac OS X's new 128x128 RGBA "thumbnail" icon format to provide
25 // richly detailed thumbnail previews of the files' contents.
27 // Using IconFamily, this becomes as simple as:
29 // id iconFamily = [IconFamily iconFamilyWithThumbnailsOfImage:anImage];
30 // [iconFamily setAsCustomIconForFile:anExistingFile];
32 // You can also write an icon family to an .icns file using the -writeToFile:
35 @interface IconFamily
: NSObject
37 IconFamilyHandle hIconFamily
;
40 // Convenience methods. These use the corresponding -init methods to return
41 // an autoreleased IconFamily instance.
43 // NOTE: +iconFamily relies on -init, which is currently broken (see -init).
45 + (IconFamily
*) iconFamily
;
46 + (IconFamily
*) iconFamilyWithContentsOfFile
:(NSString
*)path
;
47 + (IconFamily
*) iconFamilyWithIconOfFile
:(NSString
*)path
;
48 + (IconFamily
*) iconFamilyWithIconFamilyHandle
:(IconFamilyHandle
)hNewIconFamily
;
49 + (IconFamily
*) iconFamilyWithSystemIcon
:(int)fourByteCode
;
50 + (IconFamily
*) iconFamilyWithThumbnailsOfImage
:(NSImage
*)image
;
51 + (IconFamily
*) iconFamilyWithThumbnailsOfImage
:(NSImage
*)image usingImageInterpolation
:(NSImageInterpolation
)imageInterpolation
;
53 // Initializes as a new, empty IconFamily. This is IconFamily's designated
54 // initializer method.
56 // NOTE: This method is broken until we figure out how to create a valid new
57 // IconFamilyHandle! In the meantime, use -initWithContentsOfFile: to
58 // load an existing .icns file that you can use as a starting point, and
59 // use -setIconFamilyElement:fromBitmapImageRep: to replace its
60 // elements. This is what the "MakeThumbnail" demo app does.
64 // Initializes an IconFamily by loading the contents of an .icns file.
66 - initWithContentsOfFile
:(NSString
*)path
;
68 // Initializes an IconFamily from an existing Carbon IconFamilyHandle.
70 - initWithIconFamilyHandle
:(IconFamilyHandle
)hNewIconFamily
;
72 // Initializes an IconFamily by loading the Finder icon that's assigned to a
75 - initWithIconOfFile
:(NSString
*)path
;
77 // Initializes an IconFamily by referencing a standard system icon.
79 - initWithSystemIcon
:(int)fourByteCode
;
81 // Initializes an IconFamily by creating its elements from a resampled
82 // NSImage. The second form of this method allows you to specify the degree
83 // of antialiasing to be used in resampling the image, by passing in one of
84 // the NSImageInterpolation... constants that are defined in
85 // NSGraphicsContext.h. The first form of this initializer simply calls the
86 // second form with imageInterpolation set to NSImageInterpolationHigh, which
87 // produces highly smoothed thumbnails.
89 - initWithThumbnailsOfImage
:(NSImage
*)image
;
90 - initWithThumbnailsOfImage
:(NSImage
*)image usingImageInterpolation
:(NSImageInterpolation
)imageInterpolation
;
92 // Writes the icon family to an .icns file.
94 - (BOOL
) writeToFile
:(NSString
*)path
;
96 // Sets the image data for one of the icon family's elements from an
97 // NSBitmapImageRep. The "elementType" parameter must be one of the icon
98 // family element types listed below, and the format of the "bitmapImageRep"
99 // must match the corresponding requirements specified below. Regardless of
100 // the elementType, the bitmapImageRep must also be non-planar and have 8 bits
103 // elementType dimensions format
104 // ------------------- ---------- ---------------------------------------
105 // kThumbnail32BitData 128 x 128 32-bit RGBA, 32-bit RGB, or 24-bit RGB
106 // kThumbnail8BitMask 128 x 128 32-bit RGBA or 8-bit intensity
107 // kLarge32BitData 32 x 32 32-bit RGBA, 32-bit RGB, or 24-bit RGB
108 // kLarge8BitMask 32 x 32 32-bit RGBA or 8-bit intensity
109 // kLarge1BitMask 32 x 32 32-bit RGBA, 8-bit intensity, or 1-bit
110 // kSmall32BitData 16 x 16 32-bit RGBA, 32-bit RGB, or 24-bit RGB
111 // kSmall8BitMask 16 x 16 32-bit RGBA or 8-bit intensity
112 // kSmall1BitMask 16 x 16 32-bit RGBA, 8-bit intensity, or 1-bit
114 // When an RGBA image is supplied to set a "Mask" element, the mask data is
115 // taken from the image's alpha channel.
117 // NOTE: Setting an IconFamily's kLarge1BitMask seems to damage the IconFamily
118 // for some as yet unknown reason. (If you then assign the icon family
119 // as a file's custom icon using -setAsCustomIconForFile:, the custom
120 // icon doesn't appear for the file in the Finder.) However, both
121 // custom icon display and mouse-click hit-testing in the Finder seem to
122 // work fine when we only set the other four elements (thus keeping the
123 // existing kLarge1BitMask from the valid icon family from which we
124 // initialized the IconFamily via -initWithContentsOfFile:, since
125 // IconFamily's -init method is currently broken...), so it seems safe
126 // to just leave the kLarge1BitMask alone.
128 - (BOOL
) setIconFamilyElement
:(OSType
)elementType
129 fromBitmapImageRep
:(NSBitmapImageRep
*)bitmapImageRep
;
131 // Gets the image data for one of the icon family's elements as a new, 32-bit
132 // RGBA NSBitmapImageRep. The specified elementType should be one of
133 // kThumbnail32BitData, kLarge32BitData, or kSmall32BitData.
135 // The returned NSBitmapImageRep will have the corresponding 8-bit mask data
136 // in its alpha channel, or a fully opaque alpha channel if the icon family
137 // has no 8-bit mask data for the specified alpha channel.
139 // Returns nil if the requested element cannot be retrieved (e.g. if the
140 // icon family has no such 32BitData element).
142 - (NSBitmapImageRep
*) bitmapImageRepWithAlphaForIconFamilyElement
:(OSType
)elementType
;
144 // Creates and returns an NSImage that contains the icon family's various
145 // elements as its NSImageReps.
147 - (NSImage
*) imageWithAllReps
;
149 // NOTE: Planned method -- not yet implemented.
151 // Gets the image data for one of the icon family's elements as a new
152 // NSBitmapImageRep. The specified elementType should be one of
153 // kThumbnail32BitData, kThumbnail32BitMask, kLarge32BitData, kLarge8BitMask,
154 // kLarge1BitMask, kSmall32BitData, kSmall8BitMask, or kSmall1BitMask.
156 // - (NSBitmapImageRep*) bitmapImageRepForIconFamilyElement:(OSType)elementType;
158 // Writes the icon family to the resource fork of the specified file as its
159 // kCustomIconResource, and sets the necessary Finder bits so the icon will
160 // be displayed for the file in Finder views.
162 - (BOOL
) setAsCustomIconForFile
:(NSString
*)path
;
163 - (BOOL
) setAsCustomIconForFile
:(NSString
*)path withCompatibility
:(BOOL
)compat
;
165 // Same as the -setAsCustomIconForFile:... methods, but for folders (directories).
167 - (BOOL
) setAsCustomIconForDirectory
:(NSString
*)path
;
168 - (BOOL
) setAsCustomIconForDirectory
:(NSString
*)path withCompatibility
:(BOOL
)compat
;
170 // Removes the custom icon (if any) from the specified file's resource fork,
171 // and clears the necessary Finder bits for the file. (Note that this is a
172 // class method, so you don't need an instance of IconFamily to invoke it.)
174 + (BOOL
) removeCustomIconFromFile
:(NSString
*)path
;
178 // Methods for interfacing with the Carbon Scrap Manager (analogous to and
179 // interoperable with the Cocoa Pasteboard).
180 @interface
IconFamily (ScrapAdditions
)
181 + (BOOL
) canInitWithScrap
;
182 + (IconFamily
*) iconFamilyWithScrap
;